<template>
  <div class="dashboard-editor-container">

    <panel-group @handleSetLineChartData="handleSetLineChartData" />

    <el-row style="background:#fff;padding:16px 16px 0;margin-bottom:32px;">
      <line-chart :chart-data="lineChartData" />
    </el-row>

    <el-row :gutter="32">
      <el-col :xs="24" :sm="24" :lg="8">
        <div class="chart-wrapper">
          <pie-chart :chart-data="pieChartData" />
        </div>
      </el-col>
      <el-col :xs="24" :sm="24" :lg="16">
        <div class="chart-wrapper">
          <bar-chart :chart-data="barChartData" />
        </div>
      </el-col>
    </el-row>


  </div>
</template>

<script>
import PanelGroup from './dashboard/PanelGroup'
import LineChart from './dashboard/LineChart'
import PieChart from './dashboard/PieChart'
import BarChart from './dashboard/BarChart'
import { getMonthlyLineChart, getBarChart, getPieChart } from '@/api/yjnb/workspace'

export default {
  name: 'Index',
  components: {
    PanelGroup,
    LineChart,
    PieChart,
    BarChart
  },
  data() {
    return {
      lineChartData: {
        productCenter: [],
        agriculturalService: [],
        creditInfo: []
      },
      barChartData: {
        categories: [],
        values: []
      },
      pieChartData: []
    }
  },
  created() {
    this.fetchLineChartData()
    this.fetchBarChartData()
    this.fetchPieChartData()
  },
  methods: {
    //获取折线图数据
    fetchLineChartData() {
      getMonthlyLineChart().then(response => {
        if (response.code === 200 && response.data) {
          const data = response.data
          this.lineChartData.productCenter = data.productCenter || []
          this.lineChartData.agriculturalService = data.agriculturalService || []
          this.lineChartData.creditInfo = data.creditInfo || []
        }
      }).catch(error => {
        console.error('获取折线图数据失败:', error)
        this.lineChartData = {
          productCenter: [],
          agriculturalService: [],
          creditInfo: []
        }
      })
    },
    //获取柱状图数据
    fetchBarChartData() {
      getBarChart().then(response => {
        if (response.code === 200 && response.data) {
          const data = response.data
          this.barChartData.categories = data.categories || []
          this.barChartData.values = data.values || []
        }
      }).catch(error => {
        console.error('获取柱状图数据失败:', error)
        this.barChartData = {
          categories: [],
          values: []
        }
      })
    },
    //获取饼图数据
    fetchPieChartData() {
      getPieChart().then(response => {
        if (response.code === 200 && response.data) {
          const data = response.data
          this.pieChartData = data || []
        }
      }).catch(error => {
        console.error('获取饼图数据失败:', error)
        this.pieChartData = []
      })
    },
    handleSetLineChartData(type) {
      // 此方法用于切换折线图数据类型，后续如需修改可在此处调整
      // 暂时保留，可根据实际需求扩展
    }
  }
}
</script>

<style lang="scss" scoped>
.dashboard-editor-container {
  padding: 32px;
  background-color: rgb(240, 242, 245);
  position: relative;

  .chart-wrapper {
    background: #fff;
    padding: 16px 16px 0;
    margin-bottom: 32px;
  }
}

@media (max-width:1024px) {
  .chart-wrapper {
    padding: 8px;
  }
}
</style>
